package com.huaxin.hxmodulesystem.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaxin.hxmodulesystem.common.CaptchaUtil;
import com.huaxin.hxmodulesystem.dto.ResponseCode;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.AddSysUserReqDTO;
import com.huaxin.hxmodulesystem.dto.request.SysUserLoginReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdatePasswordReqDTO;
import com.huaxin.hxmodulesystem.dto.request.UpdateSysUserReqDTO;
import com.huaxin.hxmodulesystem.dto.response.SysUserLoginRespDTO;
import com.huaxin.hxmodulesystem.models.SysUser;
import com.huaxin.hxmodulesystem.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Tag(name = "系统用户管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/sysUser")
public class SysUserController {
    private final SysUserService sysUserService;

    private final CaptchaUtil captchaUtil;

    //新增用户
    @Operation(summary = "新增用户", description = "新增用户")
    @PostMapping("/addUser")
    public ResponseResult<String> addUser(@RequestBody AddSysUserReqDTO in) {
        return sysUserService.addUser(in);
    }

    //更新用户
    @Operation(summary = "更新用户", description = "更新用户")
    @PostMapping("/updateUser")
    public ResponseResult<String> updateUser(@RequestBody UpdateSysUserReqDTO in) {
        return sysUserService.updateUser(in);
    }

    //删除用户
    @Operation(summary = "删除用户", description = "删除用户")
    @GetMapping("/deleteUser")
    public ResponseResult<String> deleteUser(@Parameter(description = "用户ID", name = "userId") @RequestParam(name = "userId") String userId) {
        return sysUserService.deleteUser(userId);
    }

    //禁用用户
    @Operation(summary = "禁用用户", description = "禁用用户")
    @GetMapping("/disableUser")
    public ResponseResult<String> disableUser(@Parameter(description = "用户ID", name = "userId") @RequestParam(name = "userId") String userId,
                                              @Parameter(description = "状态", name = "status") @RequestParam(name = "status") Integer status,
                                              @Parameter(description = "锁定时间", name = "lockedUntil") @RequestParam(name = "lockedUntil") LocalDateTime lockedUntil) {
        return sysUserService.disableUser(userId, status, lockedUntil);
    }

    //获取用户列表
    @Operation(summary = "获取用户列表", description = "获取用户列表")
    @GetMapping("/getUserList")
    public ResponseResult<Page<SysUser>> getUserList(@Parameter(description = "页码", required = true, name = "pageNum") @RequestParam(value = "pageNum") Integer pageNum,
                                                     @Parameter(description = "每页数量", required = true, name = "pageSize") @RequestParam(value = "pageSize") Integer pageSize,
                                                     @Parameter(description = "搜索条件", required = false, name = "search") @RequestParam(value = "search", required = false) String search,
                                                     @Parameter(description = "用户状态", required = false, name = "status") @RequestParam(value = "status", required = false) Integer status) {
        return sysUserService.getUserList(pageNum, pageSize, search, status);
    }

    @Operation(summary = "获取所有系统用户ID", description = "获取所有系统用户ID")
    @GetMapping("/getAllSysUserIds")
    public ResponseResult<List<String>> getAllSysUserIds() {
        ResponseResult<Page<SysUser>> page = sysUserService.getUserList(1, 1000, null, null);
        if (page.getCode() != ResponseCode.SUCCESS.getCode() || page.getData() == null) {
            return ResponseResult.ok(List.of());
        }
        return ResponseResult.ok(page.getData().getRecords().stream().map(SysUser::getId).toList());
    }

    //获取用户信息
    @Operation(summary = "获取用户信息", description = "获取用户信息")
    @GetMapping("/getUserInfoById")
    public ResponseResult<SysUser> getUserInfoById(String userId) {
        return ResponseResult.ok(sysUserService.getUserInfoById(userId));
    }

    //给用户设置角色
    @Operation(summary = "给用户设置角色", description = "给用户设置角色")
    @GetMapping("/setUserRole")
    public ResponseResult<String> setUserRole(@Parameter(description = "用户ID", name = "userId") @RequestParam(name = "userId") String userId,
                                              @Parameter(description = "角色ID", name = "roleId") @RequestParam(name = "roleId") String roleId) {
        return sysUserService.setUserRole(userId, roleId);
    }

    //登录
    @Operation(summary = "登录", description = "登录")
    @PostMapping("/login")
    public ResponseResult<SysUserLoginRespDTO> login(@RequestBody SysUserLoginReqDTO in) {
        boolean isValid = captchaUtil.validateCaptcha(in.getVKey(), in.getVCode());
        if (!isValid) {
            return ResponseResult.verificationCodeError(null, "验证码错误或已过期");
        }
        return sysUserService.login(in);
    }


    //初始化密码
    @Operation(summary = "初始化密码", description = "初始化密码")
    @GetMapping("/initPassword")
    public ResponseResult<String> initPassword(@Parameter(description = "用户ID", name = "userId") @RequestParam(name = "userId") String userId) {
        return sysUserService.initPassword(userId);
    }

    //修改密码
    @Operation(summary = "修改密码", description = "修改密码")
    @PostMapping("/updatePassword")
    public ResponseResult<String> updatePassword(@RequestBody UpdatePasswordReqDTO in) {
        return sysUserService.updatePassword(in);
    }
}
